#include <graphics.h>
#include <conio.h>

//游戏画面尺寸
#define High 700
#define Width 500

#pragma comment(lib,"Winmm.lib")

IMAGE img_bk;
IMAGE img_planeNormal1, img_planeNormal2;
IMAGE img_bullet1, img_bullet2;
IMAGE img_enemyPlane1, img_enemyPlane2;
IMAGE img_planeExplode1, img_planeExplode2;

int plane_x, plane_y;			//飞机坐标
int bullet_x, bullet_y;			//子弹坐标
float enemy_x, enemy_y;			//敌机坐标
int score;						//得分
int isExplode;					//爆炸
TCHAR s[100];

void startup()
{
	mciSendString(TEXT("open D:\\game_music.mp3 alias bkmusic"), NULL, 0, NULL);//背景音乐
	mciSendString(TEXT("play bkmusic repeat"), NULL, 0, NULL);				//循环播放

	initgraph(Width, High);
	loadimage(&img_bk, _T("D:\\background2.jpg"));
	loadimage(&img_planeNormal1, _T("D:\\planeNormal_1.jpg"));
	loadimage(&img_planeNormal2, _T("D:\\planeNormal_2.jpg"));
	loadimage(&img_bullet1, _T("D:\\bullet1.jpg"));
	loadimage(&img_bullet2, _T("D:\\bullet2.jpg"));
	loadimage(&img_enemyPlane1, _T("D:\\enemyPlane1.jpg"));
	loadimage(&img_enemyPlane2, _T("D:\\enemyPlane2.jpg"));
	loadimage(&img_planeExplode1, _T("D:\\planeExplode_1.jpg"));
	loadimage(&img_planeExplode2, _T("D:\\planeExplode_2.jpg"));

	plane_x = Width * 0.5;
	plane_y = High * 0.7;

	bullet_x = plane_x;
	bullet_y = -200;

	enemy_x = Width * 0.5;
	enemy_y = 0;

	score = 0;

	isExplode = 0;

	BeginBatchDraw();
}

void show()
{
	putimage(0, 0, &img_bk);											//显示背景图片
	if (isExplode == 0)
	{
		putimage(plane_x - 50, plane_y - 60, &img_planeNormal1, NOTSRCERASE);
		putimage(plane_x - 50, plane_y - 60, &img_planeNormal2, SRCINVERT);		//显示正常飞机

		putimage(bullet_x, bullet_y, &img_bullet1, NOTSRCERASE);
		putimage(bullet_x, bullet_y, &img_bullet2, SRCINVERT);				//显示子弹
		putimage(enemy_x, enemy_y, &img_enemyPlane1, NOTSRCERASE);
		putimage(enemy_x, enemy_y, &img_enemyPlane2, SRCINVERT);			//显示敌机
		putimage(enemy_x, enemy_y, &img_enemyPlane1, NOTSRCERASE);
		putimage(enemy_x, enemy_y, &img_enemyPlane2, SRCINVERT);			//显示敌机
	}
	else
	{
		putimage(plane_x - 50, plane_y - 60, &img_planeExplode1, NOTSRCERASE);
		putimage(plane_x - 50, plane_y - 60, &img_planeExplode2, SRCINVERT);		//显示爆炸飞机
	}

	_stprintf(s, _T("得分%d"), score);
	settextstyle(40, 30, 0);
	outtextxy(Width*0.5-70, High-40, s);

	FlushBatchDraw();

	Sleep(2);

	if (isExplode == 1)
	{
		system("pause");
		exit(0);
	}
}

void updateWithoutInput()
{
	if(bullet_y>-50)
		bullet_y -= 2;				//子弹向上移动
	if (enemy_y < High)
		enemy_y=enemy_y+0.5;
	else
	{
		enemy_x = rand() % Width;
		enemy_y = 0;
	}

	if(bullet_x>=enemy_x && bullet_x<=enemy_x+104 && bullet_y<=enemy_y+148 && bullet_y>=enemy_y)
	{
		enemy_x = rand() % Width;
		enemy_y = 0;

		mciSendString(TEXT("close gotmusic"), NULL, 0, NULL);				  //把前面一次的音乐关上
		mciSendString(TEXT("open D:\\gotEnemy.mp3 alias gotmusic"), NULL, 0, NULL);//打开音乐
		mciSendString(TEXT("play gotmusic"), NULL, 0, NULL);					  //播放一次

		score++;
		if (score > 0 && score % 5 == 0 && score % 2 != 0)
		{
			mciSendString(TEXT("close 5music"), NULL, 0, NULL);				  //把前面一次的音乐关上
			mciSendString(TEXT("open D:\\5.mp3 alias 5music"), NULL, 0, NULL);//打开音乐
			mciSendString(TEXT("play 5music"), NULL, 0, NULL);					  //播放一次
		}
		if (score % 10 == 0)
		{
			mciSendString(TEXT("close 10music"), NULL, 0, NULL);				  //把前面一次的音乐关上
			mciSendString(TEXT("open D:\\10.mp3 alias 10music"), NULL, 0, NULL);//打开音乐
			mciSendString(TEXT("play 10music"), NULL, 0, NULL);					  //播放一次
		}
	}																//子弹击中敌机

	if (enemy_x<=plane_x+117 && enemy_x+104>=plane_x && enemy_y<=plane_y+120 && enemy_y+148>=plane_y)
	{
		isExplode = 1;
		mciSendString(TEXT("close exmusic"), NULL, 0, NULL);				  //把前面一次的音乐关上
		mciSendString(TEXT("open D:\\explode.mp3 alias exmusic"), NULL, 0, NULL);//打开音乐
		mciSendString(TEXT("play exmusic"), NULL, 0, NULL);					  //播放一次
	}
}

void updateWithInput()
{
	MOUSEMSG m;
	while (MouseHit())
	{
		m = GetMouseMsg();
		if (m.uMsg == WM_MOUSEMOVE)
		{
			plane_x = m.x;
			plane_y = m.y;
		}
		else if (m.uMsg == WM_LBUTTONDOWN)
		{
			mciSendString(TEXT("close f_gun"), NULL, 0, NULL);				  //把前面一次的音乐关上
			mciSendString(TEXT("open D:\\f_gun.mp3 alias f_gun"), NULL, 0, NULL);//打开音乐
			mciSendString(TEXT("play f_gun"), NULL, 0, NULL);					  //播放一次

			bullet_x = plane_x-6;
			bullet_y = plane_y-85;
		}

	}

}

void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
}
int main()
{
	startup();  // 数据初始化	
	while (1)  //  游戏循环执行
	{
		show();  // 显示新画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}